package com.sisqorap.wakeuplight;

import com.sisqorap.wakeuplight.Alarm;
import com.sisqorap.wakeuplight.R;

import android.app.Activity;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TimePicker;
import android.widget.Toast;

public class WakeUpLight extends Activity {
	private Alarm alarm;
	private Toast mToast;
	private int iHour;
	private int iMinute;
	
	static final int TIME_DIALOG_ID = 0;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        alarm = new Alarm(this);
	    
	    Button mPickTime = (Button) findViewById(R.id.set_alarm);
	    // add a click listener to the button
	    mPickTime.setOnClickListener(new View.OnClickListener() {
	        public void onClick(View v) {
	            showDialog(TIME_DIALOG_ID);
	        }
	    });	    
	    updateDisplay();
	    Button clearTimes = (Button)findViewById(R.id.btnClear);
	    clearTimes.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				//Cancel First alarm
				alarm.removeFirstAlarm();
	            updateDisplay();
			}
		});
    }
    
    private void updateDisplay() {
		Button clearBtn = (Button)findViewById(R.id.btnClear);
		Button fAlarmBtn = (Button)findViewById(R.id.set_alarm);
		if(alarm.isFirstAlarmSet()){
			fAlarmBtn.setText(getString(R.string.set_alarm) + "\n" + alarm.getFirstAlarmTime());
	        clearBtn.setEnabled(true);
        }else{
        	fAlarmBtn.setText(getString(R.string.set_alarm));
        	clearBtn.setEnabled(false);
        }
	}
    
    @Override
	protected Dialog onCreateDialog(int id) {
	    switch (id) {
	    	case TIME_DIALOG_ID:
	    		iHour = alarm.getFirstAlarmHour();
	    		Log.v("Hour", iHour + "");
	    		iMinute = alarm.getFirstAlarmMinute();
	    		Log.v("Minute", iMinute + "");
	        	return new TimePickerDialog(this, mTimeSetListener, iHour, iMinute, true);
	    }
	    updateDisplay();
	    return null;
    }
    
    // the callback received when the user "sets" the time in the dialog
	private TimePickerDialog.OnTimeSetListener mTimeSetListener =
	    new TimePickerDialog.OnTimeSetListener() {
	        public void onTimeSet(TimePicker view, int hourOfDay, int minuteSet) {
        		String alarmTime = alarm.setFirstAlarm(hourOfDay, minuteSet);
                // Tell the user about what we did.
                if (mToast != null) {
                    mToast.cancel();
                }
                mToast = Toast.makeText(WakeUpLight.this, alarmTime, Toast.LENGTH_LONG);
                mToast.show();
                updateDisplay();
	        }
	    };
	    
    @Override
	public boolean onCreateOptionsMenu(Menu menu) {
		Settings setting = new Settings(this);
	    MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.main_menu, menu);
	    MenuItem mItem = menu.findItem(R.id.sSnoozeTime5);
	    switch(setting.getSnoozeTime()){
	    	case 5:
	    		mItem = menu.findItem(R.id.sSnoozeTime5);
	    		break;
	    	case 10:
	    		mItem = menu.findItem(R.id.sSnoozeTime10);
	    		break;
	    	case 15:
	    		mItem = menu.findItem(R.id.sSnoozeTime15);
	    		break;
	    	case 30:
	    		mItem = menu.findItem(R.id.sSnoozeTime30);
	    		break;
	    }
	    mItem.setChecked(true);
	    return true;
	}
	
	@Override
	public boolean onPrepareOptionsMenu(Menu menu){
		return super.onPrepareOptionsMenu(menu);
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
    	Settings setting = new Settings(this);
	    switch (item.getItemId()) {
	        case R.id.sSnoozeTime5:
	        case R.id.sSnoozeTime10:
	        case R.id.sSnoozeTime15:
	        case R.id.sSnoozeTime30:
	        	item.setChecked(true);
	        	setting.setSnoozeTime(Short.parseShort((item.getTitle() + "").split(" ")[0]));
	        	break;
	    }
	    return true;
	}
}